var windowName = "setLocation";
var Page,bMap
apiready = function(){
    onWindowShow(function () {
        console.log(windowName + " show")
        // getData(Page)
    })
    onWindowHide(function () {
        console.log(windowName + " hide")
    })
    Page = createVM("#Page",{
        //页面数据
        type:"in",
        lon:117.991683,
        lat: 37.399625,
        name:""
    },{
        //页面方法
        "init":function () {
            var _this = this;

            var wh  = getWidthAndHeight4ele("#header")
            bMap = api.require('bMap');
            bMap.open({
                rect: {
                    x: 0,
                    y: wh.h,
                    w: api.winWidth,
                    h: api.winWidth
                },
                center: {
                    lon: Page.lon,
                    lat: Page.lat
                },
                zoomLevel: 16,
                showUserLocation: true,
                fixedOn: api.frameName,
                fixed: true
            }, function(ret) {
                if (ret.status) {
                    log('地图打开成功');
                    Page.getCurLoc()
                    bMap.addEventListener({
                        name: 'viewChange'
                    }, function(ret) {
                        if (ret.status) {
                            log(JSON.stringify(ret));
                            Page.lon = ret.lon
                            Page.lat = ret.lat
                            bMap.addAnnotations({
                                annotations: [{
                                    id: 1,
                                    lon: ret.lon,
                                    lat: ret.lat
                                }],
                                // icon: 'widget://',
                                draggable: true
                            }, function(ret) {
                                if (ret) {
                                    // alert(ret.id);
                                }
                            });
                        }
                    });
                }
            });
            setTimeout(function () {
                bMap.setZoomLevel({
                    level: 18
                });
            },1000)
        },
        getCurLoc:function () {
            bMap.getLocation({
                accuracy: '100m',
                autoStop: true,
                filter: 1
            }, function(ret, err) {
                if (ret.status) {
                    log(JSON.stringify(ret));
                    bMap.setCenter({
                        coords: {
                            lon: ret.lon,
                            lat: ret.lat
                        },
                        animation: false
                    });
                } else {
                    log("err.code:" + err.code);
                }
            });

            bMap.getCurrentLocation(
                function(ret, err) {
                    if (ret.status){
                        log("ret.status:" + JSON.stringify(ret));
                    }else {
                        log("ret.err:" + JSON.stringify(err));
                    }
                });
        },
        save: function () {
            var data = {
                'form[lon]':Page.lon,
                'form[lat]':Page.lat,
                'form[name]':Page.name,
            }
            logJson(data)
            apiPost("/app/location/add",data,function (res) {
                showToast(res.message)
            })
        }
    })
    Page.init()

}

